package shuiyuqing.views.widgets;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ScrollView;

import com.xkh.xkh.MyApplication;
import managers.UIHandler;
import shuiyuqing.activities.WaterActivity;

/**
 * 滚动条照样能够回传触摸事件
 * Created by 凯 on 2015/11/10.
 */
public class MyScrollview extends ScrollView {
    public MyScrollview(Context context) {
        super(context);
    }

    public MyScrollview(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyScrollview(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    // 监听手势，进行处理
    GestureDetector mGestureDetector = new GestureDetector(MyApplication.share(), new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

            if (Math.abs(velocityY) < 100) {
                return true;
            }

            // 手势向下 down
            if ((e2.getRawY() - e1.getRawY()) > 200) {
                System.out.println("向下");
                WaterActivity waterActivity = (WaterActivity) UIHandler.uiHandler.getActivityByName(WaterActivity.class.getSimpleName());
                if (waterActivity != null)
                {
                    waterActivity.onRefresh();
                }
                return true;
            }
            // 手势向上 up
            if ((e1.getRawY() - e2.getRawY()) > 200) {
                return true;
            }

            return super.onFling(e1, e2, velocityX, velocityY);
        }
    });


    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        super.onInterceptTouchEvent(ev);
        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        super.onTouchEvent(ev);
        mGestureDetector.onTouchEvent(ev);
        return true;
    }
}
